using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace AUSRIS.Reports.Utilities.Deployment.Logging.ConsoleTextFileLogger
{
	public class Logger : ConsoleLogger.Logger
	{
		private StringBuilder output = new StringBuilder();
		private string fileName;

		public string FileName
		{
			get
			{
				return fileName;
			}
			set
			{
				fileName = value;
			}
		}
		public override void LogException(Exception ex)
		{
			base.LogException(ex);
			this.output.AppendLine();
			this.output.Append("Exception:");
			this.output.AppendLine(ex.ToString());
			this.output.AppendLine();
		}

		public override void LogNewLine()
		{
			base.LogNewLine();
			this.output.AppendLine();
		}

		public override void LogMessage(string message)
		{
			base.LogMessage(message);
			if ( this.ShowTimestamp )
			{
				this.output.AppendFormat("{0}{1}{2}", DateTime.Now.TimeOfDay.ToString(), '\t', message);
			}
			else
			{
				this.output.AppendLine(message);
			}
		}

		public override void Flush()
		{
			if ( !string.IsNullOrEmpty(this.fileName) )
			{
				Console.WriteLine();
				File.WriteAllText(this.fileName, this.output.ToString());
				Console.WriteLine("Saved log to " + this.fileName);
			}
		}
	}
}
